package cn.dw.web.action;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

import org.json.JSONObject;

import com.baidu.aip.face.AipFace;

public class BaiduFace {

	
	
	//设置APPID/AK/SK
    public static final String APP_ID = "9944006";
    public static final String API_KEY = "3EZv2KcgjiGw1jmqg8nfPhPk";
    public static final String SECRET_KEY = "qdFX92nRiMh5dyGSpjamzgNWMEWxtEls";
	
	public static void main(String[] args) {
		faceRecognize(null);
	}

	
	
	 public static String dowloadfile(String url_) throws IOException, Throwable {
			
		    //	url_ = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=BjfUholx6_OqUA23hCqg-k9-EeavXMATKfP340I8WkNLaFFiRScOCui06C_fgJJOu5hAqVkblfq2Xt0qN3dMoh939k_j8nHU4ucrgo0N6Bsly8FWcFzly5BdN5YWQFIsRUQaACAGJF&media_id=u-xGNBS4g19Tant3g7QM4mt93fI5OfTcTWAYBACHoGRM3kR7FN_CLHdOvbEt6grq";
		    	
		    	URL url = new URL(url_);
		    	
		    	HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
		    	
		    	//防止屏蔽程序抓取而返回403错误
		        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

		     //   URI uri = new URI(url_);
		        
		        String path = "C:\\local\\";
		        
		        //得到输入流
		        InputStream inputStream = conn.getInputStream();  
		        
		        byte[]  getData = readInputStream(inputStream);
		        
		        File saveDir = new File(path);
		        
		        if(!saveDir.exists()){
		            saveDir.mkdir();
		        }
		        
		        File file = new File(path+"123.jpg");
		        
		        FileOutputStream fos = new FileOutputStream(file);
		        
		        fos.write(getData);
		        
		        if(fos!=null){
		            fos.close();  
		        }
		        if(inputStream!=null){
		            inputStream.close();
		        }
		        
				return faceRecognize(path+"123.jpg");
		    	
			}    
	 
	 
	 /**
	     * 从输入流中获取字节数组
	     * @param inputStream
	     * @return
	     * @throws IOException
	     */
	    public static  byte[] readInputStream(InputStream inputStream) throws IOException {  
	        byte[] buffer = new byte[1024];  
	        int len = 0;  
	        ByteArrayOutputStream bos = new ByteArrayOutputStream();  
	        while((len = inputStream.read(buffer)) != -1) {  
	            bos.write(buffer, 0, len);  
	        }  
	        bos.close();  
	        return bos.toByteArray();  
	    }  
	
	
	public static String faceRecognize(String filepath) {
		
		 // 初始化一个FaceClient
		AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
        
        
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

		
	    // 自定义参数定义
		HashMap<String, String> options = new HashMap<String, String>();
	    options.put("max_face_num", "1");
	    options.put("face_fields", "age,beauty,expression,gender");

	    
	    
	    // 参数为本地图片路径
	  //  String imagePath = "D:\\360安全浏览器下载\\9.jpg";
	    String imagePath = filepath;
	    JSONObject response = client.detect(imagePath, options);
	    
	    String result = "";
	    
	 //   System.out.println(response.toString());
	    
	    JSONObject res = new JSONObject(response.get("result").toString().replace("[", "").replace("]", "").trim());
	    
	//    System.out.println(res.get("beauty").toString());
	    
	    Double beauty_num =new Double(res.get("beauty").toString().trim()) ;
	    
	    if(beauty_num > 0 && beauty_num <= 20){
	    	result = "你的颜值低到该去重新投胎了！！！\n"
	    			+ "测试可能不准啊！！唉！唉！不要打脸啊！！！";
	    } else if (beauty_num >20 && beauty_num <= 40) {
			result = "你的颜值还是不要出来吓小朋友了！吓不到小朋友，吓坏花花草草也不好啊！！\n"
	    			+ "测试可能不准啊！！唉！唉！不要打脸啊！！！";
		} else if (beauty_num >40 && beauty_num <= 60) {
			result = "你的颜值勉强对得起观众了。。。\n"
	    			+ "测试可能不准啊！！唉！唉！不要打脸啊！！！";
		} else if (beauty_num > 60 && beauty_num <= 80) {
			result = "你的颜值好高啊，羡煞本宝宝了！！\n"
					+"测试不一定是准的，自己偷乐就行了，就不要告诉你的基友了吧！！！";
		} else if (beauty_num > 80 && beauty_num <=100) {
			result = "你的颜值可以秒杀众人，亮瞎了我的24k钛合金大眼啊！！！！啊！！！\n"
					+ "这个测试没那么准，也可能是假的！！";
		}
	    
	 //   System.out.println(response.toString());
	    
	    System.out.println(result);

	    return result;
	    
	    // 参数为本地图片文件二进制数组
	//    byte[] file = readImageFile(imagePath);
	 //   JSONObject response = client.detect(file, options);
	//    System.out.println(response.toString());
	}


	
	
}
